<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Doctor extends MY_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('general_model');
        $this->per_page = 1;
        $data['doctor'] = $this->general_model->get_row('doctors', "user_id = {$this->user_id}");
    }

    public function index() {

        parent::core_doctor_view('doctor/body_view');
    }

    function list_pat($page = false) {
        /*         * pagination */
        $row = $this->general_model->get('patients', '', '', '', '', 'count');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'date', $group_by = false, '');

        //echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('doctor/list_pat', $data);
    }

    function check_pat($id = false, $action = false) {
        if ($id == false) {
            redirect('doctor');
        }
        if (urldecode($action) == 'ตรวจก่อนทำสวยงามแล้ว' or urldecode($action) == 'ตรวจก่อนทำสวยงาม') {
            redirect('doctor/check_pat_before/' . $id);
            exit();
        } elseif (urldecode($action) == 'ตรวจหลังทำสวยงามแล้ว' or urldecode($action) == 'ตรวจหลังทำสวยงาม') {
            redirect('doctor/check_pat_after/' . $id);
            exit();
        } elseif (urldecode($action) == 'ประเมิณ') {
            redirect('doctor/check_evol/' . $id);
            exit();
        }
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id}  order by check_id DESC limit 1 "); ##ดึงเฉพาะล่าสุด
        if ($data['check'][0]['check_date'] == '' or $data['check'][0]['check_date'] == '0000-00-00 00:00:00') {
            $check_status = array("รอตรวจก่อนทำสวยงาม", "รอตรวจหลังทำสวยงาม", "เริ่มบริการ รอการตรวจร่างกาย");
            if (in_array($data['check'][0]['check_status'], $check_status)) {
                $data['cheked'] = 1;
            } else {
                $data['cheked'] = 2;
            }
        } else {
            $data['cheked'] = false;
        }
        if ($data['check'][0] != false) {
            parent::core_doctor_view('doctor/check_pat', $data);
        } else {

            redirect('doctor/list_pat');
        }
    }

    function query_check_pat($id = false, $check_id = false) {##id ผู้ผ่วย เลือกค่าตรวจล่าสุดมา
        if ($id == false) {
            redirect('doctor');
        }

        if (urldecode($check_id) == 'รอตรวจก่อนทำสวยงาม') {
            redirect('doctor/query_check_pat_before/' . $id);
            exit();
        } elseif (urldecode($check_id) == 'รอตรวจหลังทำสวยงาม' or urldecode($check_id) == 'ตรวจก่อนทำสวยงามแล้ว') {
            redirect('doctor/query_check_pat_after/' . $id);
            exit();
        } elseif (urldecode($check_id) == 'รอการประเมิณ' or urldecode($check_id) == 'ตรวจหลังทำสวยงามแล้ว') {
            redirect('doctor/check_evol/' . $id);
            exit();
        }
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} and check_status !='ประเมิณเรียบร้อยแล้ว' order by check_id DESC limit 1 ");
        if ($this->input->post()) {
            $post = $this->input->post();
            array_pop($post);
            unset($post['order_type']);
            unset($post['tool_id']);
            unset($post['order_cause']);
            unset($post['neck']);
            if ($check_id == false) {
                if ($type != 'edit') {
                    $post['check_status'] = 'กำลังจัดทำอุปกรณ์';
                }
                $this->general_model->insert('check_pat', $post);
                $check_id = $this->db->insert_id();
            } else {
                if ($type != 'edit') {
                    $post['check_status'] = 'กำลังจัดทำอุปกรณ์';
                }
                $this->general_model->update('check_pat', $post, "check_id = {$check_id}");
            }


            $post = $this->input->post();
            array_pop($post);

            foreach ($post as $key => $value) {
                $check_id = $check_id;
                if ($key == 'order_type' or $key == 'tool_id' or $key == 'order_cause' or $key == 'neck') {

                    if ($key == 'order_type') {
                        foreach ($value as $k1 => $v1) {

                            $order_type[$k1] = $v1;
                        }
                    } elseif ($key == 'tool_id') {
                        foreach ($value as $k2 => $v2) {
                            $tool_id[$k2] = $v2;
                        }
                    } elseif ($key == 'order_cause') {
                        foreach ($value as $k3 => $v3) {
                            $order_cause[$k3] = $v3;
                        }
                    } elseif ($key == 'neck') {
                        foreach ($value as $k3 => $v3) {
                            $neck[$k3] = $v3;
                        }
                    }
                }
            }
            if ($check_id != false) {
                $this->general_model->del('order_tools', "check_id = {$check_id}");
            }
            $i = 0;
            while ($i < count($order_type)) {
                $data_tool = array(
                    'check_id' => $check_id,
                    'order_type' => $order_type[$i],
                    'tool_id' => $tool_id[$i],
                    'order_cause' => $order_cause[$i],
                    'neck' => $neck[$i],
                );
                $this->general_model->insert('order_tools', $data_tool);
                $i++;
            }

            redirect('doctor/check_pat/' . $id . '/' . $action);
        }



        parent::core_doctor_view('doctor/check_pat_add_edit', $data);
    }

    function check_pat_before($id = false, $action = false) {
        if ($id == false) {
            redirect('doctor');
        }

        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1"); ##ดึงเฉพาะล่าสุด
        if ($data['check'][0]['date_check_before'] == '' or $data['check'][0]['date_check_before'] == '0000-00-00 00:00:00') {
            $check_status = array("รอตรวจก่อนทำสวยงาม", "รอตรวจหลังทำสวยงาม", "เริ่มบริการ รอการตรวจร่างกาย");
            if (in_array($data['check'][0]['check_status'], $check_status)) {
                $data['cheked'] = 1;
            } else {
                $data['cheked'] = 2;
            }
        } else {
            $data['cheked'] = false;
        }


        if ($data['check'][0] != false) {
            parent::core_doctor_view('doctor/check_pat_before', $data);
        } else {

            $ref = $this->input->server('HTTP_REFERER', TRUE);
            ?>
            <meta charset="utf-8">
            <script type="text/javascript">
                var url = '<?php echo $ref; ?>';
                if (confirm(" ยังไม่ได้รับการตรวจร่างกาย !") == true) {
                    window.location.href = url;
                } else {
                    window.location.href = url;
                }

            </script>

            <?php
        }
    }

    function query_check_pat_before($id = false, $check_id = false, $type = false) {##id ผู้ผ่วย เลือกค่าตรวจล่าสุดมา
        if ($id == false) {
            redirect('doctor');
        }

        if (urldecode($check_id) == 'รอตรวจก่อนทำสวยงาม') {
            redirect('doctor/check_pat_before/' . $id);
            exit();
        } elseif (urldecode($check_id) == 'edit') {
            $data['type'] = 'edit';
        } else {
            $data['type'] = false;
        }
        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1");

        if ($this->input->post()) {
            $post = $this->input->post();
            array_pop($post);
            unset($post['order_type']);
            unset($post['tool_id']);
            unset($post['order_cause']);
            if ($check_id == false) {
                $this->general_model->insert('check_pat', $post);
                $check_id = $this->db->insert_id();
            } else {
                if ($type != 'edit') {
                    $post['check_status'] = 'ตรวจก่อนทำสวยงามแล้ว';
                }
                $this->general_model->update('check_pat', $post, "check_id = {$check_id}");
            }



            redirect('doctor/check_pat_before/' . $id);
        }



        parent::core_doctor_view('doctor/check_pat_before_add_edit', $data);
    }

    function check_pat_after($id = false, $action = false) {
        if ($id == false) {
            redirect('doctor');
        }

        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1"); ##ดึงเฉพาะล่าสุด
        if ($data['check'][0]['date_check_after'] == '' or $data['check'][0]['date_check_after'] == '0000-00-00 00:00:00') {
            $check_status = array("รอตรวจก่อนทำสวยงาม", "รอตรวจหลังทำสวยงาม", "เริ่มบริการ รอการตรวจร่างกาย");
            if (in_array($data['check'][0]['check_status'], $check_status)) {
                $data['cheked'] = 1;
            } else {
                $data['cheked'] = 2;
            }
        } else {
            $data['cheked'] = false;
        }
        if ($data['check'][0] != false) {
            parent::core_doctor_view('doctor/check_pat_after', $data);
        } else {
            redirect('patient/list_pat');
        }
    }

    function query_check_pat_after($id = false, $check_id = false, $type = false) {##id ผู้ผ่วย เลือกค่าตรวจล่าสุดมา
        if ($id == false) {
            redirect('doctor');
        }

        if (urldecode($check_id) == 'edit') {
            $data['type'] = $type;
        } else {
            $data['type'] = false;
        }



        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1");
        if ($type != false) {
            $data['type'] = 'edit';
        } else {
            $data['type'] = false;
        }
        if ($this->input->post()) {
            $post = $this->input->post();
            array_pop($post);
            unset($post['order_type']);
            unset($post['tool_id']);
            unset($post['order_cause']);
            if ($check_id == false) {
                $this->general_model->insert('check_pat', $post);
                $check_id = $this->db->insert_id();
            } else {
                if ($type == false && $type != 'edit') {
                    $post['check_status'] = 'ตรวจหลังทำสวยงามแล้ว';
                }
                $this->general_model->update('check_pat', $post, "check_id = {$check_id}");
                redirect('doctor/check_pat_after/' . $id);
            }
        }



        parent::core_doctor_view('doctor/check_pat_after_add_edit', $data);
    }

    function check_evol($id = false, $action = false) {
        if ($id == false) {
            redirect('doctor');
        }

        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1"); ##ดึงเฉพาะล่าสุด
        if ($data['check'][0] != false) {
            $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด

            $check_status = array("รอการประเมิณ", "ประเมิณท่าเดินแล้ว", "ประเมิณก่อนมอบให้คนพิการแล้ว", "ประเมิณเรียบร้อยแล้ว");
            if (in_array($data['check'][0]['check_status'], $check_status)) {
                $data['cheked'] = 1;
            } elseif ($data['check'][0]['check_status'] == 'ตรวจหลังทำสวยงามแล้ว') {
                $data['cheked'] = 2;
            } else {
                $data['cheked'] = false;
            }

            parent::core_doctor_view('doctor/evol', $data);
        } else {
            redirect('patient/list_pat');
        }



        if ($data['evol'][0]['evol_move_result'] != '' && $data['evol'][0]['evol_before_result'] != '') {
            $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด
            if ($data['evol'][0]['evol_move_result'] != '' && $data['evol'][0]['evol_before_result'] != '') {
                $param['check_status'] = 'ประเมิณเรียบร้อยแล้ว';
            }

            //var_dump($param['check_status']);die();

            $this->general_model->update('check_pat', $param, "check_id = {$data['check'][0]['check_id']}");
        }
    }

    function query_check_evol($id = false, $check_id = false) {##id ผู้ผ่วย เลือกค่าตรวจล่าสุดมา
        if ($id == false) {
            redirect('doctor');
        }

        $data['pat'] = $this->general_model->get_row('patients', "pat_id = {$id}");
        $data['check'][] = $this->general_model->get_row('check_pat', "pat_id = {$id} order by check_id DESC limit 1");
        $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด
        if ($this->input->post()) {
            $post = $this->input->post();
            $post['pat_id'] = $data['pat']['pat_id'];
            $post['check_id'] = $data['check'][0]['check_id'];
            unset($post['submit']);


            if (empty($data['evol'][0]) or $data['check'][0]['status'] == 'ประเมิณก่อนมอบให้คนพิการแล้ว') {
                $this->general_model->insert('evolution', $post);
                $evol_id = $this->db->insert_id();
                $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด
                if ($data['evol'][0]['evol_move_result'] != '' && $data['evol'][0]['evol_before_result'] != '') {
                    $param['check_status'] = 'ประเมิณเรียบร้อยแล้ว';
                } elseif ($this->input->get('evol') == 1) {
                    $param['check_status'] = 'ประเมิณท่าเดินแล้ว';
                } elseif ($this->input->get('evol') == 2) {
                    $param['check_status'] = 'ประเมิณก่อนมอบให้คนพิการแล้ว';
                }
                //var_dump($param['check_status']);die();

                $this->general_model->update('check_pat', $param, "check_id = {$data['check'][0]['check_id']}");
            } else {
                $this->general_model->update('evolution', $post, "check_id = {$data['check'][0]['check_id']} and pat_id = {$data['pat']['pat_id'] } ");
                $data['evol'][] = $this->general_model->get_row('evolution', "pat_id = {$id} and check_id = {$data['check'][0]['check_id']}"); ##ดึงเฉพาะล่าสุด
                if ($data['evol'][0]['evol_move_result'] != '' && $data['evol'][0]['evol_before_result'] != '') {
                    $param['check_status'] = 'ประเมิณเรียบร้อยแล้ว';
                } elseif ($this->input->get('evol') == 1) {
                    $param['check_status'] = 'ประเมิณท่าเดินแล้ว';
                } elseif ($this->input->get('evol') == 2) {
                    $param['check_status'] = 'ประเมิณก่อนมอบให้คนพิการแล้ว';
                }
                //var_dump($param['check_status']);die();

                $this->general_model->update('check_pat', $param, "check_id = {$data['check'][0]['check_id']}");
            }
            redirect(base_url() . 'doctor/check_evol/' . $id, '');
            exit();
        }


        parent::core_doctor_view('doctor/evol_add_edit', $data);
    }

    ####
}
